//designpatterns.flyweight.simple.IgoChessmanFactory.java
package org.patterns.structural.flyweight.simple;


import java.util.Hashtable;

//围棋棋子工厂类：享元工厂类，使用单例模式进行设计
public class IgoChessmanFactory {
    // 定义颜色常量，避免魔法字符串
    public static final String COLOR_BLACK = "black";
    public static final String COLOR_WHITE = "white";

    private static final IgoChessmanFactory instance = new IgoChessmanFactory();
    //使用Hashtable来存储享元对象，充当享元池
    private static final Hashtable<String, IgoChessman> hashtable = new Hashtable<>();

    static {
        // 初始化享元对象，放入享元池
        hashtable.put(COLOR_BLACK, new BlackIgoChessman());
        hashtable.put(COLOR_WHITE, new WhiteIgoChessman());
    }

    private IgoChessmanFactory() {
        // 私有构造函数防止外部实例化
    }

    //返回享元工厂类的唯一实例
    public static IgoChessmanFactory getInstance() {
        return instance;
    }

    //通过key来获取存储在Hashtable中的享元对象
    public static IgoChessman getIgoChessman(String color) {
        IgoChessman chessman = hashtable.get(color);
        if (chessman == null) {
            throw new IllegalArgumentException("Invalid chessman color: " + color);
        }
        return chessman;
    }
}